package client.comm.baoding.ui

import android.content.Intent
import android.view.View
import client.comm.commlib.widget.EmptyLayout
import client.comm.baoding.R
import client.comm.baoding.api.bean.TdBean
import client.comm.baoding.databinding.ActivityTdBinding
import client.comm.baoding.ui.vm.TdViewModel
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.adapter.Td2Adapter
import client.comm.baoding.adapter.Td2Sub2Adapter
import client.comm.baoding.adapter.Td2SubAdapter
import client.comm.baoding.databinding.ActivityTdSub2Binding
import client.comm.baoding.databinding.ActivityTdSubBinding

/**
 * 2
 */
class TdSub2Activity : BaseMiddleActivity<TdViewModel, ActivityTdSub2Binding>() {

    override fun getLayout(): Int {
        return R.layout.activity_td_sub2
    }

    val adapter: Td2Sub2Adapter by lazy {
        Td2Sub2Adapter(this, mImgLoader)
    }

    override fun initView() {
        binding.event = this
        binding.recyclerView.adapter = adapter
//        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))
        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true
            viewModel.pageOne.value = 0
            viewModel.get_user_team()
        }
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.get_user_team()
            }
        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })

        binding.emptyLayout.setOnClickListener {
            viewModel.get_user_team()
        }

        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list = it.data?.sub_list as MutableList<TdBean.Sub?>
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                        binding.title = "${it.data?.nickname}的代理"
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 10) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }

            }
        })
    }


    override fun initData() {
        intent.getStringExtra("bean").apply {

            viewModel.bean.value = this
            adapter.hasMoreState.value = true
            viewModel.pageOne.value = 0
            viewModel.get_user_team()
        }

    }

    override fun initViewModel(): TdViewModel = createViewModel()

    /**
     * 奖励记录
     */
    fun jljl() {
        startActivity(Intent(this, TdJlJlActivity::class.java))
    }

}